﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using EnvDTE;
using Magellan.ProjectWizards.Framework;
using Magellan.ProjectWizards.Setup;
using Magellan.ProjectWizards.Setup.SetupTemplate;
using Microsoft.VisualStudio.TemplateWizard;
using Microsoft.Win32;

namespace Magellan.ProjectWizards
{
    public class SetupWizard : IWizard
    {
        private List<string> _projects;
        private DTE _environment;

        public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            _environment = (DTE)automationObject;

            try
            {
                // Prepare the model
                var model = new SetupModel();
                model.CopyLibraries = true;
                model.CreateTestProject = true;
                model.LibraryDirectory = Path.GetFullPath(Path.Combine(replacementsDictionary["$destinationdirectory$"], "..\\Dependencies"));
                model.TestProjectName = replacementsDictionary["$safeprojectname$"] + ".Tests";
                model.VersionNumber = Assembly.GetExecutingAssembly().GetName().Version.ToString();

                // Show the screen
                var dialog = new SetupForm(model);
                var result = dialog.ShowDialog();
                if (result == DialogResult.Cancel)
                {
                    throw new WizardCancelledException();
                }

                // Execute the model
                var runner = new SetupRunner();
                runner.DependenciesSourceDirectory = Path.Combine((string)Registry.LocalMachine.GetValue("Software\\Magellan\\Dependencies"), "Desktop");
                runner.Execute(model, replacementsDictionary);
                _projects = runner.CreatedProjects;
            }
            catch (WizardCancelledException)
            {
                throw;
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("An unexpected error has occurred: {0}", ex));
                Trace.WriteLine(ex);
            }
        }

        public void ProjectFinishedGenerating(Project project)
        {
        }

        public void ProjectItemFinishedGenerating(ProjectItem projectItem)
        {
        }

        public bool ShouldAddProjectItem(string filePath)
        {
            return true;
        }

        public void BeforeOpeningFile(ProjectItem projectItem)
        {
        }

        public void RunFinished()
        {
            if (_projects == null) return;
            if (_environment == null) return;
            if (_environment.Solution == null) return;
            
            // Connect the created projects
            foreach (var project in _projects)
            {
                _environment.Solution.AddFromFile(project, false);
            }
            _environment = null;
        }
    }
}
